package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.zhijian.medical.dao.local.ExperimentReportMapper;
import com.zhijian.medical.entity.dto.ExperimentReportJcxmDataDto;
import com.zhijian.medical.entity.dto.ExperimentReportLsjcDto;
import com.zhijian.medical.entity.dto.ExperimentReportQcmxDto;
import com.zhijian.medical.entity.po.ExperimentReport;
import com.zhijian.medical.entity.vo.request.ExperimentReportSearchVo;
import com.zhijian.medical.entity.vo.request.ExperimentReportUpdateVo;
import com.zhijian.medical.entity.vo.request.PrintTemplateSearchVo;
import com.zhijian.medical.entity.vo.response.ExperimentReportListVo;
import com.zhijian.medical.entity.vo.response.ExperimentReportVo;
import com.zhijian.medical.entity.vo.response.ExperimentWithTemplateVo;
import com.zhijian.medical.entity.vo.response.PrintTemplateVo;
import com.zhijian.medical.service.ExperimentReportService;
import com.zhijian.medical.service.PcrResultDetailService;
import com.zhijian.medical.service.PrintTemplateService;
import com.zhijian.medical.util.JsonUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 实验报告业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-07-15 17:05:10
 */
@Slf4j
@Service
public class ExperimentReportServiceImpl implements ExperimentReportService {

    @Autowired
    private ExperimentReportMapper mapper;

    @Autowired
    private PcrResultDetailService pcrResultDetailService;

    @Autowired
    private PrintTemplateService printTemplateService;

    @Override
    public ExperimentReport selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ExperimentReportVo selectVoById(Long id) {
        ExperimentReportVo experimentReportVo = mapper.selectVoById(id);
        if (Objects.isNull(experimentReportVo)) {
            return null;
        }
        //样本id
        Long sampleId = experimentReportVo.getSampleId();
        List<ExperimentReportVo> experimentReportVos = this.selectList(ExperimentReportSearchVo.builder()
                .sampleId(sampleId)
                .build());
        //历史检查
        if (CollectionUtil.isNotEmpty(experimentReportVos)) {
            //把当前的记录去掉
            List<ExperimentReportVo> unRepeat = experimentReportVos.stream().filter(e -> !Objects.equals(e.getId(), experimentReportVo.getId())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(unRepeat)) {
                List<ExperimentReportLsjcDto> collect = unRepeat.stream().map(u -> {
                    ExperimentReportLsjcDto experimentReportLsjcDto = new ExperimentReportLsjcDto();
                    experimentReportLsjcDto.setAge(u.getAge());
                    experimentReportLsjcDto.setBgsj(StringUtils.isNotBlank(u.getCreateTime()) ? u.getCreateTime().split(" ")[0] : "");
                    experimentReportLsjcDto.setBlh(u.getBlh());
                    experimentReportLsjcDto.setId(u.getId() + "");
                    return experimentReportLsjcDto;
                }).collect(Collectors.toList());
                experimentReportVo.setLsjcList(collect);
            }
        }
        String brafdata = experimentReportVo.getBrafdata();
        if (StringUtils.isNotBlank(brafdata)) {
            experimentReportVo.setBrafdataList(JsonUtil.toArray(brafdata, ExperimentReportJcxmDataDto.class));
        }
        String krasdata = experimentReportVo.getKrasdata();
        if (StringUtils.isNotBlank(krasdata)) {
            experimentReportVo.setKrasdataList(JsonUtil.toArray(krasdata, ExperimentReportJcxmDataDto.class));
        }
        String nrasdata = experimentReportVo.getNrasdata();
        if (StringUtils.isNotBlank(nrasdata)) {
            experimentReportVo.setNrasdataList(JsonUtil.toArray(nrasdata, ExperimentReportJcxmDataDto.class));
        }

        //所有的扩增曲线
        experimentReportVo.setKzLineFilePathList(pcrResultDetailService.selectKzLineFilePathListByBlh(experimentReportVo.getFzblh()));
        return experimentReportVo;
    }

    @Override
    public ExperimentWithTemplateVo selectExperimentWithTemplateVoById(Long id) {
        ExperimentWithTemplateVo experimentWithTemplateVo = new ExperimentWithTemplateVo();
        //暂时只找淮安的
        List<PrintTemplateVo> printTemplateVos = printTemplateService.selectList(PrintTemplateSearchVo.builder()
                .name("淮安")
                .build());
        if (CollectionUtil.isNotEmpty(printTemplateVos)) {
            experimentWithTemplateVo.setPrintTemplate(printTemplateVos.get(0));
        }

        ExperimentReportVo experimentReportVo = this.selectVoById(id);
        if (Objects.isNull(experimentReportVo)) {
            experimentWithTemplateVo.setData(ExperimentWithTemplateVo.DataVo.builder().build());
            return experimentWithTemplateVo;
        }
        //赋值
        ExperimentWithTemplateVo.DataVo dataVo = new ExperimentWithTemplateVo.DataVo();
        BeanUtils.copyProperties(experimentReportVo, dataVo);
        if (Objects.nonNull(experimentReportVo.getSex())) {
            //性别
            dataVo.setXb(experimentReportVo.getSex() == 0 ? "男" : "女");
        }
        dataVo.setXm(experimentReportVo.getName());
        dataVo.setNl(experimentReportVo.getAge());
        dataVo.setSdrq(experimentReportVo.getReceiveDate());
        dataVo.setSjyy(experimentReportVo.getHospital());
        dataVo.setSjys(experimentReportVo.getDoctor());


        List<String> kzLineFilePathList = experimentReportVo.getKzLineFilePathList();
        if (CollectionUtil.isNotEmpty(kzLineFilePathList)) {
            //扩增曲线图
            //阴阳性质控图
            dataVo.setYyxzkt(kzLineFilePathList.get(0));
            //样本检测图
            dataVo.setYbjct(kzLineFilePathList.get(kzLineFilePathList.size() - 1));
        }
        //braf
        List<ExperimentReportJcxmDataDto> brafdataList = experimentReportVo.getBrafdataList();
        //nras
        List<ExperimentReportJcxmDataDto> nrasdataList = experimentReportVo.getNrasdataList();
        //kras
        List<ExperimentReportJcxmDataDto> krasdataList = experimentReportVo.getKrasdataList();
        List<ExperimentReportJcxmDataDto> experimentReportJcxmDataDtoList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(brafdataList)) {
            experimentReportJcxmDataDtoList.addAll(brafdataList);
        }
        if (CollectionUtil.isNotEmpty(nrasdataList)) {
            experimentReportJcxmDataDtoList.addAll(nrasdataList);
        }
        if (CollectionUtil.isNotEmpty(krasdataList)) {
            experimentReportJcxmDataDtoList.addAll(krasdataList);
        }
        if (CollectionUtil.isNotEmpty(experimentReportJcxmDataDtoList)) {
            dataVo.setJcjg(experimentReportJcxmDataDtoList.stream().map(e -> {
                ExperimentWithTemplateVo.DataVo.Jcjg jcjg = new ExperimentWithTemplateVo.DataVo.Jcjg();
                BeanUtils.copyProperties(e, jcjg);
                return jcjg;
            }).collect(Collectors.toList()));
        } else {
            dataVo.setJcjg(Collections.emptyList());
        }
        experimentWithTemplateVo.setData(dataVo);
        return experimentWithTemplateVo;
    }

    @Override
    public ExperimentReportVo selectNewestVo() {
        ExperimentReportVo experimentReportVo = mapper.selectNewestVo();
        if (Objects.isNull(experimentReportVo)) {
            return null;
        }
        String lsjc = experimentReportVo.getLsjc();
        if (StringUtils.isNotBlank(lsjc)) {
            //历史检查列表
            experimentReportVo.setLsjcList(JsonUtil.toArray(lsjc, ExperimentReportLsjcDto.class));
        }
        String qcmx = experimentReportVo.getQcmx();
        if (StringUtils.isNotBlank(qcmx)) {
            //取材明细列表
            experimentReportVo.setQcmxList(JsonUtil.toArray(qcmx, ExperimentReportQcmxDto.class));
        }
        String krasdata = experimentReportVo.getKrasdata();
        if (StringUtils.isNotBlank(krasdata)) {
            //KRAS数据
            experimentReportVo.setKrasdataList(JsonUtil.toArray(krasdata, ExperimentReportJcxmDataDto.class));
        }
        String nrasdata = experimentReportVo.getNrasdata();
        if (StringUtils.isNotBlank(nrasdata)) {
            //NRAS数据
            experimentReportVo.setNrasdataList(JsonUtil.toArray(nrasdata, ExperimentReportJcxmDataDto.class));
        }
        String brafdata = experimentReportVo.getBrafdata();
        if (StringUtils.isNotBlank(brafdata)) {
            //BRAF数据
            experimentReportVo.setBrafdataList(JsonUtil.toArray(brafdata, ExperimentReportJcxmDataDto.class));
        }
        return experimentReportVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByTypeAndExperimentProcessId(Byte type, Long experimentProcessId) {
        mapper.deleteByTypeAndExperimentProcessId(type, experimentProcessId);
    }

    @Override
    public Long insert(ExperimentReport experimentReport) {
        Date now = new Date();
        experimentReport.setCreateTime(now);
        experimentReport.setUpdateTime(now);
        return mapper.insert(experimentReport) > 0 ? experimentReport.getId() : 0L;
    }

    @Override
    public boolean update(ExperimentReport experimentReport) {
        Date now = new Date();
        experimentReport.setUpdateTime(now);
        return mapper.updateByPrimaryKey(experimentReport) != 0;
    }

    @Override
    public boolean update(ExperimentReportUpdateVo experimentReportUpdateVo) {
        ExperimentReport experimentReport = new ExperimentReport();
        BeanUtils.copyProperties(experimentReportUpdateVo, experimentReport);
        String receiveDate = experimentReportUpdateVo.getReceiveDate();
        if (StringUtils.isNotBlank(receiveDate)) {
            experimentReport.setReceiveDate(DateUtil.parseDate(receiveDate));
        }
        List<ExperimentReportLsjcDto> lsjcList = experimentReportUpdateVo.getLsjcList();
        if (CollectionUtil.isNotEmpty(lsjcList)) {
            //历史检查
            experimentReport.setLsjc(JsonUtil.toString(lsjcList));
        }
        List<ExperimentReportQcmxDto> qcmxList = experimentReportUpdateVo.getQcmxList();
        if (CollectionUtil.isNotEmpty(qcmxList)) {
            //取材明细
            experimentReport.setQcmx(JsonUtil.toString(qcmxList));
        }
        List<ExperimentReportJcxmDataDto> krasdataList = experimentReportUpdateVo.getKrasdataList();
        if (CollectionUtil.isNotEmpty(krasdataList)) {
            //KRAS数据
            experimentReport.setKrasdata(JsonUtil.toString(krasdataList));
        }
        List<ExperimentReportJcxmDataDto> nrasdataList = experimentReportUpdateVo.getNrasdataList();
        if (CollectionUtil.isNotEmpty(nrasdataList)) {
            //NRAS数据
            experimentReport.setNrasdata(JsonUtil.toString(nrasdataList));
        }
        List<ExperimentReportJcxmDataDto> brafdataList = experimentReportUpdateVo.getBrafdataList();
        if (CollectionUtil.isNotEmpty(brafdataList)) {
            //NRAS数据
            experimentReport.setBrafdata(JsonUtil.toString(brafdataList));
        }
        return this.update(experimentReport);
    }

    @Override
    public Integer count(ExperimentReportSearchVo experimentReportSearchVo) {
        return mapper.count(experimentReportSearchVo);
    }

    @Override
    public List<ExperimentReportVo> selectList(ExperimentReportSearchVo experimentReportSearchVo) {
        return mapper.selectList(experimentReportSearchVo);
    }

    @Override
    public Page<ExperimentReportVo> selectPage(ExperimentReportSearchVo experimentReportSearchVo) {
        //先直接查总数
        int count = count(experimentReportSearchVo);
        //分页
        Page<ExperimentReportVo> page = new Page<>(true, count, experimentReportSearchVo.getPageNum(), experimentReportSearchVo.getPageSize());
        experimentReportSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExperimentReportVo> responseVos = this.selectList(experimentReportSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public ExperimentReportVo selectNewestVoBySearch(ExperimentReportSearchVo experimentReportSearchVo) {
        return mapper.selectNewestVoBySearch(experimentReportSearchVo);
    }

    @Override
    public boolean insertBatch(List<ExperimentReport> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public List<ExperimentReportListVo> selectExperimentReportListVoBySearch(ExperimentReportSearchVo experimentReportSearchVo) {
        return mapper.selectExperimentReportListVoBySearch(experimentReportSearchVo).stream().peek(s -> s.setMainType(experimentReportSearchVo.getMainType())).collect(Collectors.toList());
    }

}